package service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.ArrayList;

public class ServiceThread extends Thread{
	Socket client;
	String player;
	BufferedReader in ;
	PrintStream out;
	boolean connected ;
	
	public ServiceThread(Socket socket){
		this.client = socket;
		this.connected=true;
	}
	public void run(){
		try {
			in =  new BufferedReader(new InputStreamReader(client.getInputStream()));
			out = new PrintStream(client.getOutputStream());
			while(enter()==false){
				if(this.connected==false) return ;
			}
			ArrayList<String> temp = ServiceMain.dbop.getPlayer(player);
			for(int i=0;i<temp.size();i++){
				out.println(temp.get(i));
//				System.out.println(temp.get(i));
			}
			ServiceMain.userSocketMap.put(player, client);
			
			while(true){
				String str = readNext();
				if(str.equals("session")){
					dealSession();
				}
				else if(str.equals("fight")){
					dealFight();
				}
				else if(str.equals("update")){
					str = readNext();
					ServiceMain.dbop.update(str);
				}
				else if(str.equals("delete")){
					str = readNext();
					ServiceMain.dbop.delete(str);
				}
				else if(str.equals("insert")){
					str = readNext();
//					System.out.println(str);
					ServiceMain.dbop.insert(str);
				}
				else if(str.equals("exit")) break;
				
				if(this.connected==false) ;
			}
			ServiceMain.userSocketMap.remove(player);
//			System.out.println(ServiceMain.userSocketMap.size());
		} catch (Exception e) {
			System.out.print(e.getMessage());
		};
		if(player!=null&&ServiceMain.userSocketMap.containsKey(player))
			ServiceMain.userSocketMap.remove(player);
	}
	
	public boolean dealFight(){
		try{
			
			return true ;
		}catch(Exception e){
			
		}
		return false;
	}
	
	
	public boolean dealSession(){
		try{
			String str = readNext();
			String[] temp = str.split(" ");
			String to = temp[0];
			String text = temp[1];
			
			PrintStream out = new PrintStream(ServiceMain.userSocketMap.get(temp[0]).getOutputStream()); 
			out.println(text);
			return true;
		}catch(Exception e){
		}
		return false;
	}
	
	
	
	
	public boolean enter() {
		try{
			String str = readNext();
			if(str.equals("login")){
//				System.out.println("?");
				str = readNext();
//				System.out.println(str);
//				System.out.println("?");
				if(ServiceMain.dbop.logIn(str)){
					if(ServiceMain.userSocketMap.containsKey(str.split(" ")[0])){
						out.println("No");
						return false;
					}
					out.println("Yes");
					this.player=str.split(" ")[0];
					return true;
				}
			}
			else if(str.equals("register")){
				str = readNext();
				if(ServiceMain.dbop.register(str)){
					if(ServiceMain.userSocketMap.containsKey(str.split(" ")[0])){
						out.println("No");
						return false;
					}
					out.println("Yes");
					this.player=str.split(" ")[0];
					return true;
				}
			}
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		out.println("No");
		return false;
	}
	String readNext(){
		String str = null;
		try {
			str = in.readLine();
		} catch (Exception e) {
			this.connected = false;
		}
		return str ;
	}

}
